<?php
namespace app\admin\controller;

use think\Controller;
use think\paginator\driver\Bootstrap;
use think\Url;
class Tasks extends Controller
{
    public function index()
    {
        $keyword = trim(input('get.keyword'));
        if ($keyword != ''){
            $tasks_list=db('tasks')->field(['id','task','task_decr','if_on'])->where('task_decr','like','%'.$keyword.'%')->paginate(10);
        }else {
            $tasks_list = db('tasks')->field(['id','task','task_decr','if_on'])->paginate(10);
        }
        $page = $tasks_list->render();
        
        $this->assign('task_list',$tasks_list);
        $this->assign('page',$page);
        return $this->fetch('index');
    }
    
    public function save_task()
    {
        $task = trim(input('get.task'));
        if ($task == '' || $task == false){
            $this->assign('action','add');
            return $this->fetch('save_task');
        }
        $task_info = db('tasks')->field(['id','task','task_decr','exc_type','time','exc_time'])->where('task',$task)->find();
        $this->assign('task',$task_info);
        $this->assign('action','edit');
        return $this->fetch('save_task');  
    }
    
    public function del_task()
    {
    	$result = array('err' => '1','content' => '');
    	$task_id = input('post.id');
    	$del_test = db('tasks')->where('id',$task_id)->delete();
    	if ($del_test == false){
    		$result['content'] = '删除任务失败！';
    		return $result;
    	}
    	$result['content'] = '删除任务成功！';
    	$result['err'] = '0';
    	return $result;
    	
    }
    
    public function save()
    {
        $result = array('err'=>'1','content'=>'');
        $task_info = input('post.');
        $rules = [
            'task' => 'require|alphaDash|max:20',
            'task_decr' => 'require|max:20',
            'exc_type' => 'require|number',
            'time' => 'require|number'
        ];
        $message = [
            'task.require' => '任务不能为空！',
            'task.alphaDash' => '任务必须由字母、数字以及下划线组成！',
            'task.max' => '任务的字数请控制在20以内！',
            'task_decr.require' => '描述不得为空！',
            'task_decr.max' => '描述的字数请控制在20以内',
            'exc_type.require' => '执行类型不得为空！',
            'exc_type.number' => '执行类型参数错误！',
            'time.require' => '执行间隔时间不得为空！',
            'time.number' => '执行间隔时间参数有误！'
        ];
        $check_info = $this->validate($task_info, $rules,$message);
        if ($check_info != 'true'){
            $result['content'] = $check_info;
            return $result;
        }
        $tasks_model = model('Tasks');
        $save_task = $tasks_model->save_task($task_info);
        if ($save_task == false){
        	$result['content'] = '保存任务失败！';
        	return $result;
        }
        $result['err'] = '0';
        $result['content'] = '保存任务成功！';
        return $result;
    }
    
    public function task_log()
    {
        $task = trim(input('get.task'));
        $task_info = db('tasks')->field(['task','task_decr','time','exc_log'])->where('task',$task)->find();
        $log_list = $task_info['exc_log'];
        $log_list = json_decode($log_list,true);
        /** php5才支持这个函数*array_column()*/
//         array_multisort(array_column($log_list, 'time'),SORT_DESC,$log_list);

        $time_arr = array();
        foreach ($log_list as $lk => $lv){
        	array_push($time_arr, $lv['time']);
        }
        array_multisort($time_arr,SORT_DESC,$log_list);
        $curpage = input('page') ? input('page') : 1;
        $data = $log_list;
        $data_to = array_chunk($data, 20);
        $log_list = array();
        if ($data_to){
            $log_list = $data_to[$curpage-1];
        }else {
            $log_list = null;
        }
        $log_list = Bootstrap::make($log_list, 20,$curpage,count($data),false,[
            'var_page' =>'page',
            'path' => Url::build('task_log'),
            'query' => request()->param(),
            'fragment' => ''
        ]);
        $log_list->appends($_GET);
        $page = $log_list->render();
        unset($task_info['exc_log']);
        $this->assign('task_info',$task_info);
        $this->assign('log_list',$log_list);
        $this->assign('page',$page);
        return $this->fetch('task_log');
    } 
    public function del_log_task()
    {
        $if_on = input('get.if_on');
        $task = input('get.task');
    	$task_model = model('Tasks');
    	ob_end_clean();
    	ob_start();
    	$result = $task_model->task_handle($if_on, $task);
    	echo $result;
    	$size = ob_get_length();
	 	header("Content-Length: $size");
 		header('Connection: close');
 		ob_flush();
 		flush();
 		ob_end_clean();
    	$task_model->del_log($task);
    }
    
    public function del_tmp_file_task()
    {
    	$if_on = input('get.if_on');
        $task = input('get.task');
    	$task_model = model('Tasks');
    	ob_end_clean();
    	ob_start();
    	$result = $task_model->task_handle($if_on, $task);
    	echo $result;
    	$size = ob_get_length();
	 	header("Content-Length: $size");
 		header('Connection: close');
 		ob_flush();
 		flush();
 		ob_end_clean();
    	$task_model->del_tmp_file($task);
    }
    
    public function db_backup_task()
    {
    	$if_on = input('get.if_on');
    	$task = input('get.task');
    	$task_model = model('Tasks');
    	ob_end_clean();
    	ob_start();
    	$result = $task_model->task_handle($if_on, $task);
    	echo $result;
    	$size = ob_get_length();
	 	header("Content-Length: $size");
 		header('Connection: close');
//  		ob_end_flush();
 		ob_flush();
 		flush();
 		ob_end_clean();
    	$task_model->db_backup($task);
    }
    
    public function del_tmp_data_task()
    {
    	$if_on = input('get.if_on');
    	$task = input('get.task');
    	$task_model = model('Tasks');
    	ob_end_clean();
    	ob_start();
    	$result = $task_model->task_handle($if_on,$task);
    	echo $result;
    	$size = ob_get_length();
	 	header("Content-Length: $size");
 		header('Connection: close');
 		ob_flush();
 		flush();
 		ob_end_clean();
    	$task_model->del_tmp_data($task);
    }
    
    
    
    
}